package com.ynrcc.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import com.eova.config.EovaConfig;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class ImportWj {
	// 复制文件
	public static void copyFile(File sourceFile, File targetFile) throws IOException {
		// 新建文件输入流并对它进行缓冲
		FileInputStream input = new FileInputStream(sourceFile);
		BufferedInputStream inBuff = new BufferedInputStream(input);

		// 新建文件输出流并对它进行缓冲
		FileOutputStream output = new FileOutputStream(targetFile);
		BufferedOutputStream outBuff = new BufferedOutputStream(output);

		// 缓冲数组
		byte[] b = new byte[1024 * 50];
		int len;
		while ((len = inBuff.read(b)) != -1) {
			outBuff.write(b, 0, len);
		}
		// 刷新此缓冲的输出流
		outBuff.flush();

		// 关闭流
		inBuff.close();
		outBuff.close();
		output.close();
		input.close();
	}

	// 复制文件夹
	public static void copyDirectiory(String sourceDir, String targetDir) throws IOException {
		// 新建目标目录
		(new File(targetDir)).mkdirs();
		// 获取源文件夹当前下的文件或目录
		File[] file = (new File(sourceDir)).listFiles();
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {
				// 源文件
				File sourceFile = file[i];
				// 目标文件
				File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
				copyFile(sourceFile, targetFile);
			}
			if (file[i].isDirectory()) {
				// 准备复制的源文件夹
				String dir1 = sourceDir + "/" + file[i].getName();
				// 准备复制的目标文件夹
				String dir2 = targetDir + "/" + file[i].getName();
				copyDirectiory(dir1, dir2);
			}
		}
	}

	public static boolean importWj(String basedir, String sourceId, String brchcd) {
		String path = basedir + sourceId + "/wj";
		// 文件不存在则返回
		File Directory = new File(path);
		if (!Directory.exists()) {
			return true;
		}

		// 判断记录是否已经存在
		String sql = "select * from ATTACHMENT where source_id = '" + sourceId + "'";
		List<Record> attachments = Db.find(sql);
		if (attachments.size() > 0) {
			System.out.println("记录已存在，导入失败");
			return false;
		}

		// 取档案主文档ID
		sql = "select * from DOC where SOURCE_ID = '" + sourceId + "'";
		List<Record> recs = Db.find(sql);
		if (recs.size() == 0) {
			System.out.println("档案主文档ID不存在，导入失败");
			return false;
		}
		Record rec = recs.get(0);
		int fileid = rec.getInt("ID");

		// 生成文件存贮路径，并复制文件
		String storage = rec.getInt("FILE_YEAR") + "/" + rec.getStr("BRCHCD") + "/" + rec.getStr("PACK_ID") + "/";
		String dest = EovaConfig.props.get("filedb") + storage;
		File tmpFile = new File(dest);
		if (!tmpFile.exists()) {
			tmpFile.mkdirs();
		}
		try {
			copyDirectiory(path, dest);
		} catch (IOException e) {
			e.printStackTrace();
		}

		for (File file : Directory.listFiles()) {
			// 取主键
			sql = "select SEQ_ATTACHMENT.nextval SEQ from dual";
			int seq = Db.findFirst(sql).getInt("SEQ");

			Record attachment = new Record().set("ID", seq).set("BRCHCD", brchcd);
			attachment.set("FILE_ID", fileid).set("SOURCE_ID", sourceId);
			attachment.set("NAME", file.getName()).set("PATH", storage);
			Db.save("attachment", attachment);
		}
		return true;
	}
}
